<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar @confirm="search" @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="(item, id) in searchResults" :key="id" @click="gotoDetails(item)">
        <view class="goods_name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="17"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史区 -->
    <view class="histroy-box" v-else>
      <view class="histroy-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistroy"></uni-icons>
      </view>
      <view class="histroy-item">
        <uni-tag :text="item" v-for="(item, i) in histroys" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 防抖标识符
        timer: null,
        kw: '',
        // 搜索结果列表
        searchResults: [],
        // 搜索历史假数据
        histroyList: ['a', 'ab', 'abc']
      };
    },
    onLoad() {
      this.histroyList = JSON.parse(uni.getStorageSync('key' || '[]'))
    },
    methods: {
      input(e) {
        // 防抖处理
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e.value
          this.getSearchList()
        }, 500)
      },
      async getSearchList() {
        if(!this.kw) {
          this.searchResults = []
          return
        }
        const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
        this.saveSearchHistory()
      },
      saveSearchHistory() {
        // this.histroyList.push(this.kw)
        const set = new Set(this.histroyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.histroyList = Array.from(set)
        
        uni.setStorageSync('key', JSON.stringify(this.histroyList))
      },
      // 点击跳转至商品详情
      gotoDetails(item) {
        uni.navigateTo({
          url: '/api/public/v1/goods/detail?goods_id=' + item.goods_id
        })
      },
      // 点击垃圾桶图标清空历史数据
      clearHistroy() {
        this.histroyList = []
        uni.setStorageSync('key', '[]')
      }
    },
    // 点击历史数据项跳转至商品列表页面
    gotoGoodsList(item) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?query=' + item
      })
    },
    computed: {
      histroys() {
        return [...this.histroyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  
  .sugg-list {
    padding: 0 5px;
    
    .sugg-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      font-size: 12px;
      
      .goods_name {
        // 超出文本用三个点显示
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        
        margin-right: 3px;
      }
    }
  }
  
  .histroy-box {
    padding: 0 5px;
    
    .histroy-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      border-bottom: 1px solid #efefef;
      font-size: 12px;
      
      .histroy-item {
        display: flex;
        flex-wrap: wrap;
        
        .uni-tag {
          margin: 5px 5px 0 0;
        }
      }
    }
  }
</style>
